<?php
/**
 * Created by JetBrains PhpStorm.
 * User: bicho
 * Date: 16/06/12
 * Time: 13:56
 * To change this template use File | Settings | File Templates.
 */
class BaseController extends Zend_Controller_Action
{

    public function init()
    {
        $url = Zend_Controller_Front::getInstance()->getRequest()->getBaseUrl();
        $root = '/' . trim($url, '/');
        if ('/' == $root) {
            $root = '';
        }
        $this->view->url = $root . '/';
    }

    /**
     * @param  $extendedMapper
     * return all entries from the extendedMapper
     */
    public function indexAction($extendedMapper)
    {
        $this->view->entries = $extendedMapper->fetchAll();
    }


    public function formAction(Zend_Form $extendedForm,BaseModel $extendedModel,BaseMapper $extendedMapper, $redirect = true)
    {
        $request = $this->getRequest();
        $id = $request->getParam("id");

        if ($request->isPost()) {
            if ($extendedForm->isValid($request->getPost())) {
                if ($id != null) {
                    //Update only for values data
                    $values = $extendedForm->getValues();
                } else {
                    $values = new $extendedModel($extendedForm->getValues());
                }
                if($redirect){
                    $extendedMapper->save($values, $id);
                    return $this->_helper->redirector('index');
                }else{
                    return $extendedMapper->save($values, $id);
                }
            }
        } else if ($id != null) {
            $values = $extendedMapper->find($request->getParam("id"));
            $extendedForm->populate($values->toArray());
        }
        $this->view->form = $extendedForm;
    }

    /**
     * @param $extendedMapper
     * return Json list of fetchAll from extendedMapper
     */
    public function listAction($extendedMapper)
    {
        $entries = $extendedMapper->fetchAll();

        $data = array();
        foreach ($entries as $entry) {
            array_push($data, $entry->toArray());
        }

        $result = array(
            "page" => 1,
            "total" => 1,
            "records" => 3,
            "rows" => $data
        );

        echo Zend_Json::encode($result);
        exit();
    }

    public function removeAction($extendedMapper)
    {
        $request = $this->getRequest();
        if (($id = $request->getParam('id')) != null) {
            $extendedMapper->getDbTable()->delete('id=' . $id);
        }
        return $this->_helper->redirector('index');
    }

    public function postDispatch()
    {

        if (isset($_SESSION['message'])) {
            $this->view->message = $_SESSION['message'];
            $_SESSION['message'] = null;
        }
    }

    public function doPaginator($list_to_paginate, $page)
    {

        $page == null ? 1 : $page;
        $rows_page = 12;
        $range_page = 5;

        $paginador = Zend_Paginator::factory($list_to_paginate);
        $paginador->setItemCountPerPage($rows_page)
            ->setCurrentPageNumber($page)
            ->setPageRange($range_page);
        return $paginador;
    }

    public function flashMessage($msg, $success = true)
    {
        $_SESSION['message'] = array('success' => $success, 'value' => $msg);
    }
}
